﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;

namespace DonetArxLib.DoubleClickHandler
{
    public static class EditorSelectionExtension
    {
        /// <summary>
        /// 扩展方法：获取当前鼠标位置的实体
        /// </summary>
        /// <returns>返回选择集</returns>
        public static PromptSelectionResult SelectAtPickBox(this Editor ed, Point3d pickBoxCenter)
        {
            System.Windows.Point screePt = ed.PointToScreen(pickBoxCenter, 1);

            object pBox = Application.GetSystemVariable("PICKBOX");
            int pSize = Convert.ToInt32(pBox);

            Point3dCollection points = new Point3dCollection();

            // 根据pickBoxCenter和pSize计算出选择框的范围
            System.Windows.Point p;
            Point3d pt;

            p = new System.Windows.Point(screePt.X - pSize, screePt.Y - pSize);
            pt = ed.PointToWorld(p, 1);
            points.Add(pt);

            p = new System.Windows.Point(screePt.X + pSize, screePt.Y - pSize);
            pt = ed.PointToWorld(p, 1);
            points.Add(pt);

            p = new System.Windows.Point(screePt.X + pSize, screePt.Y + pSize);
            pt = ed.PointToWorld(p, 1);
            points.Add(pt);

            p = new System.Windows.Point(screePt.X - pSize, screePt.Y + pSize);
            pt = ed.PointToWorld(p, 1);
            points.Add(pt);

            return ed.SelectCrossingPolygon(points);
        }
    }
}
